package com.vgooo.client.ui;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.vgooo.bean.Area;
import com.vgooo.bean.Brand;
import com.vgooo.client.R;

public class SortAdapter extends BaseAdapter {
	private Map<Integer , View> views ;
	public List<TextView> tvs ; 
	private List brands ;
	private int layoutId,tvId ;
	private View hasMore ;
	private Context context ;
	private LayoutInflater inflater ;
	private boolean isFist ;
	private int counts ;


	public SortAdapter(List brands, int layoutId, int tvId,
			View hasMore, Context context) {
		super();
		setBrands(brands);
		this.layoutId = layoutId;
		this.tvId = tvId;
		this.hasMore = hasMore;
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		this.isFist = true ;
		this.counts = brands.size();
		views = new HashMap<Integer, View>() ;
		tvs = new ArrayList<TextView>() ;
	}




	public void setBrands(List<Brand> brands) {
		this.brands = brands;
	}


	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return counts;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return brands.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		convertView = views.get(position);
		TextView tv = null ;
		if(convertView == null){
			convertView = inflater.inflate(layoutId, null);
			tv = (TextView) convertView.findViewById(tvId);
			views.put(position, convertView);
			tvs.add(tv);
			if(isFist && !tvs.isEmpty()){
				tvs.get(0).setBackgroundResource(R.drawable.sort_text_select_bg);
				isFist = false ;
			}
		}else{
			tv =tvs.get(position);
		}
		
		if(!views.isEmpty()&&counts >= views.size()&&position <counts-1){
			if(hasMore!=null)
			hasMore.setVisibility(View.VISIBLE);
		}else{
			if(hasMore!=null)
			hasMore.setVisibility(View.GONE);
		}
		
		Brand t_brands;
		Area t_areas;
		if (brands.get(position) instanceof Brand)
		{
			t_brands = (Brand)brands.get(position);
			tv.setText("   " + t_brands.brand_name + "   ");
		}
		else if (brands.get(position) instanceof Area)
		{
			t_areas = (Area)brands.get(position);
			tv.setText("   " + t_areas.brand_name + "   ");
		}

		return convertView;
	}

}
